using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Utilities;
//using Services.Menu.Resources;
//using System.Globalization;

namespace Services.Menu
{
    public enum DateSelectorType
    {
        YearsMouthsDays,
        YearsMouths,
        Years,
        Mouths,
        MouthsDays,
        Days
    }
    
    public class DateSelectorScreen : ASelectorScreen
    {
        DateSelectorType type;
        int starting_year;
        int month = -1;
        const int years_to_show = 11;
        ArrayList<String> months_names, days_names;
        public DateSelectorScreen(Game game, Action<DateTime> closeSelector, Action abortSelection,
            DateTime defaultDateTime, DateSelectorType type, String titleString, ArrayList<string> months_names, ArrayList<string> days_names)
            : base(game, closeSelector, abortSelection, 5, defaultDateTime.Month-1, defaultDateTime.Day-1, (SelectorType)type, titleString)
        {
            this.type = type;
            starting_year = defaultDateTime.Year;
            if (type == DateSelectorType.Days)
                month = defaultDateTime.Month;
            this.months_names = months_names;
            this.days_names = days_names;
        }

        public override void Initialize()
        {
            
            base.Initialize();
            
        }

        public int getDayNameIndex()
        {
            var date = GetDateTime();
            date = date.AddDays(-date.Day + 1);
            return (int)date.DayOfWeek;
        }

        public int getMaxDay(){

            int max_day = 31;
            int m = selectedBValue;
            if (month != -1)
                m = month-1;

            switch (m)
            {
                case 0:
                case 2:
                case 4:
                case 6:
                case 7:
                case 9:
                case 11:
                    max_day = 31;
                    break;
                case 1:
                    max_day = 28;
                    int offset = years_to_show / 2;
                    int year = starting_year - offset + selectedAValue;
                    if (year % 400 == 0)
                    {
                        max_day = 29;
                    }
                    else if (year % 100 == 0)
                    {
                        max_day = 28;
                    }
                    else if (year % 4 == 0)
                        max_day = 29;
                    break;
                case 3:
                case 5:
                case 8:
                case 10:
                    max_day = 30;
                    break;
            }
            return max_day;
        }

        public int getMonthMaxDay(int m, int year)
        {            
            switch (m)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    return 31;
                case 2:
                    int max_day = 28;
                    
                    if (year % 400 == 0)
                    {
                        max_day = 29;
                    }
                    else if (year % 100 == 0)
                    {
                        max_day = 28;
                    }
                    else if (year % 4 == 0)
                        max_day = 29;
                    return max_day;
                    //break;
                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
            }
            return 28;
        }

        int block_size_default = 140;
        int year_width_default = 200;
        int year_height_default = 120;
        public override void SetPortraitOrientation()
        {

            
            listA.Clear();
            listB.Clear();
            listC.Clear();
            
            int center_x = 240;
            int center_y = 400;
            CreateLists(center_x, center_y);
            
            UpdateSelectedValue();
        }

        public override void SetLandScapeOrientation()
        {
            
            listA.Clear();
            listB.Clear();
            listC.Clear();
            int center_x = 400;
            int center_y = 240;
            CreateLists(center_x, center_y);
            
            UpdateSelectedValue();
        }
        private void CreateLists(int center_x, int center_y)
        {
            int block_size = block_size_default;
            int year_width = year_width_default;
            int year_height = year_height_default;
            if (type == DateSelectorType.Years || type == DateSelectorType.YearsMouths || type == DateSelectorType.YearsMouthsDays)
                block_size = year_height;
            switch (type)
            {
                case DateSelectorType.Years:
                case DateSelectorType.Mouths:
                case DateSelectorType.Days:
                    if (type == DateSelectorType.Years)
                    {
                        int offset = years_to_show / 2;
                        for (int i = 0; i < years_to_show; i++)
                        {
                            listA.Add(new MyButton(new Rectangle(center_x - year_width / 2, center_y - year_height / 2 + (year_height + 10) * i, year_width, year_height),
                               "" + (starting_year - offset + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        }
                        areaA = new Rectangle(center_x - year_height / 2, center_y - year_height / 2, year_width, year_height);
                        dragAreaA = new Rectangle(center_x - year_height / 2, upper_box.rectangle.Height,
                            year_width, center_y * 2 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                    }
                    else if (type == DateSelectorType.Mouths)
                    {
                        for (int i = 0; i < 12; i++)
                            listB.Add(new MyButton(new Rectangle(center_x - block_size / 2, center_y - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                "" + (i + 1), Color.White, font, TextPos.Center, months_names[i],Color.White,small, TextPos.Left, box, Color.LightGray));
                        areaB = new Rectangle(center_x - block_size / 2, center_y - block_size / 2, block_size, block_size);
                        dragAreaB = new Rectangle(center_x - block_size / 2, upper_box.rectangle.Height,
                            block_size, center_y * 2 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                    }
                    else if (type == DateSelectorType.Days)
                    {
                        int max_day = getMaxDay();
                        int index = getDayNameIndex();
                        for (int i = 0; i < max_day; i++)
                            listC.Add(new MyButton(new Rectangle(center_x - block_size / 2, center_y - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                "" + (i + 1), Color.White, font, TextPos.Center, days_names[(index + i)%7], Color.White, small, TextPos.Left, box, Color.LightGray));
                        areaC = new Rectangle(center_x - block_size / 2, center_y - block_size / 2, block_size, block_size);
                        dragAreaC = new Rectangle(center_x - block_size / 2, upper_box.rectangle.Height,
                            block_size, center_y * 2 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                    }

                    break;
                case DateSelectorType.YearsMouths:
                case DateSelectorType.MouthsDays:
                    if (type == DateSelectorType.YearsMouths)
                    {
                        int offset = years_to_show / 2;
                        for (int i = 0; i < years_to_show; i++)
                            listA.Add(new MyButton(new Rectangle(center_x - 30, center_y - year_height / 2 + (year_height + 10) * i, year_width, year_height),
                                "" + (starting_year - offset + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        areaA = new Rectangle(center_x - 30, center_y - year_height / 2, year_width, year_height);
                        dragAreaA = new Rectangle(center_x - 30, upper_box.rectangle.Height,
                            year_width, center_y * 2 - upper_box.rectangle.Height - upper_box.rectangle.Height);

                        for (int i = 0; i < 12; i++)
                            listB.Add(new MyButton(new Rectangle(center_x - 40 - block_size, center_y - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                "" + (i + 1), Color.White, font, TextPos.Center, months_names[i],Color.White,small, TextPos.Left, box, Color.LightGray));
                        areaB = new Rectangle(center_x - 40 - block_size, center_y - block_size / 2, block_size, block_size);
                        dragAreaB = new Rectangle(center_x - 40 - block_size, upper_box.rectangle.Height,
                            block_size, center_y * 2 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                    }
                    else
                    {

                        for (int i = 0; i < 12; i++)
                            listB.Add(new MyButton(new Rectangle(center_x + 5, center_y - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                "" + (i + 1), Color.White, font, TextPos.Center, months_names[i], Color.White, small, TextPos.Left, box, Color.LightGray));
                        areaB = new Rectangle(center_x + 5, center_y - block_size / 2, block_size, block_size);
                        dragAreaB = new Rectangle(center_x + 5, upper_box.rectangle.Height,
                            block_size, center_y * 2 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                        int max_day = getMaxDay();
                        int index = getDayNameIndex();
                        for (int i = 0; i < max_day; i++)
                            listC.Add(new MyButton(new Rectangle(center_x - 5 - block_size, center_y - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                "" + (i + 1), Color.White, font, TextPos.Center, days_names[(index + i) % 7], Color.White, small, TextPos.Left, box, Color.LightGray));
                        areaC = new Rectangle(center_x - 5 - block_size, center_y - block_size / 2, block_size, block_size);
                        dragAreaC = new Rectangle(center_x - 5 - block_size, upper_box.rectangle.Height,
                            block_size, center_y * 2 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                    }
                    break;
                case DateSelectorType.YearsMouthsDays:
                    {
                        int offset = years_to_show / 2;
                        for (int i = 0; i < years_to_show; i++)
                        {
                            listA.Add(new MyButton(new Rectangle(center_x + 30, center_y - year_height / 2 + (year_height + 10) * i, year_width, year_height),
                               "" + (starting_year - offset + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        }
                        areaA = new Rectangle(center_x + 30, center_y - year_height / 2, year_width, year_height);
                        dragAreaA = new Rectangle(center_x + 30, upper_box.rectangle.Height,
                            year_width, center_y * 2 - upper_box.rectangle.Height - upper_box.rectangle.Height);


                        for (int i = 0; i < 12; i++)
                            listB.Add(new MyButton(new Rectangle(center_x - 100, center_y - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                "" + (i + 1), Color.White, font, TextPos.Center, months_names[i], Color.White, small, TextPos.Left, box, Color.LightGray));
                        areaB = new Rectangle(center_x - 100, center_y - block_size / 2, block_size, block_size);
                        dragAreaB = new Rectangle(center_x - 100, upper_box.rectangle.Height,
                            block_size, center_y * 2 - upper_box.rectangle.Height - upper_box.rectangle.Height);

                        int max_day = getMaxDay();
                        int index = getDayNameIndex();
                        for (int i = 0; i < max_day; i++)
                            listC.Add(new MyButton(new Rectangle(center_x - 230, center_y - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                "" + (i + 1), Color.White, font, TextPos.Center, days_names[(index + i) % 7], Color.White, small, TextPos.Left, box, Color.LightGray));
                        areaC = new Rectangle(center_x - 230, center_y - block_size / 2, block_size, block_size);
                        dragAreaC = new Rectangle(center_x - 230, upper_box.rectangle.Height,
                            block_size, center_y * 2 - upper_box.rectangle.Height - upper_box.rectangle.Height);

                        break;
                    }
            }
        }

        DateTime GetDateTime()
        {
            int a = selectedAValue;
            int b = selectedBValue;
            int c = selectedCValue;
            DateTime dateTime;
            //if (selectedAValue <= 0)
            //    selectedAValue = DateTime.Now.Year;
            //else
            selectedAValue = DateTime.Now.Year - years_to_show/2 + selectedAValue;
            if (selectedBValue < 0)
                selectedBValue = DateTime.Now.Month;
            else
                selectedBValue++;
            if (selectedCValue < 0)
                selectedCValue = DateTime.Now.Day;
            else
                selectedCValue++;
            int offset = years_to_show / 2;
            int year = starting_year - offset + selectedAValue;
            selectedCValue = Math.Min(selectedCValue, getMonthMaxDay(selectedBValue,year));
            switch (type)
            {
                case DateSelectorType.Years:
                    dateTime = new DateTime(selectedAValue, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
                    break;
                case DateSelectorType.YearsMouths:
                    dateTime = new DateTime(selectedAValue, selectedBValue, DateTime.Now.Day, 0, 0, 0);
                    break;
                case DateSelectorType.YearsMouthsDays:
                    dateTime = new DateTime(selectedAValue, selectedBValue, selectedCValue, 0, 0, 0);
                    break;
                case DateSelectorType.Mouths:
                    dateTime = new DateTime(DateTime.Now.Year, selectedBValue, DateTime.Now.Day, 0, 0, 0);
                    break;
                case DateSelectorType.MouthsDays:
                    dateTime = new DateTime(DateTime.Now.Year, selectedBValue, selectedCValue, 0, 0, 0);
                    break;
                case DateSelectorType.Days:
                    dateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, selectedCValue, 0, 0, 0);
                    break;
                default:
                    dateTime = DateTime.Now;

                    break;
            }
            selectedAValue = a;
            selectedBValue = b;
            selectedCValue = c;
            return dateTime;
        }

        public override void ExitSelector()
        {
            closeSelector(GetDateTime());
        }

        protected override void SetSelectedValues()
        {
            base.SetSelectedValues();

            int max_day = getMaxDay();

            ChangeOrientation();


            if (selectedBValue == 1)
            {
                
                for (int i = max_day; i < listC.Count; i++)
                {
                    listC[i].enabled = false;
                }
                
                selectedCValue = SearchSelected(listC, areaC, true, Math.Min(listC.Count-1, Math.Min(max_day-1, selectedCValue)));
            }
        }
    }
}
